package com.wuhao.code.check.model.base

import com.aegis.kotlin.startsWithAny
import com.aegis.mybatis.xmlless.annotations.XmllessMethod
import com.aegis.mybatis.xmlless.enums.QueryType
import com.aegis.mybatis.xmlless.model.base.BaseXmllessMethodWrapper
import com.intellij.psi.PsiElement
import com.wuhao.code.check.getStringAnnoValue
import com.wuhao.code.check.getStringListAnnotationValue
import com.wuhao.code.check.hasAnnotation
import com.wuhao.code.check.model.KtMethodWrapper

/**
 *
 * @author 吴昊
 * @date 2024/1/24 10:32
 * @since v0.0.0
 * @version 1.0
 */
abstract class BasePsiXmllessMethodWrapper(private val fn: PsiElement) : BaseXmllessMethodWrapper {

  override fun getPropertyMapping(): Map<String, String> {
    fun String.replaceNoIfPrefix(): String {
      return if (this.startsWithAny("noif:", "noif ")) {
        this.drop(5).trim()
      } else {
        this
      }
    }
    return getIncludesInner().map {
      it.replaceNoIfPrefix().replace(" AS ", " as ").split(" as ")
    }.filter { it.size == 2 }.associate {
      it[1] to it[0]
    }
  }

  override fun getType(): QueryType? {
    val typeString = if (fn.hasAnnotation(KtMethodWrapper.METHOD_ANNO)) {
      fn.getStringAnnoValue(
          XmllessMethod::class.java.name,
          XmllessMethod::type.name
      ) ?: QueryType.None.name
    } else {
      null
    }
    return if (typeString.isNullOrBlank()) {
      null
    } else {
      QueryType.valueOf(typeString)
    }
  }

  override fun getSorts(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::sortBy.name
    )
  }

  override fun getConditions(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::conditions.name
    )
  }

  override fun getGroupBys(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::groupBy.name
    )
  }

  override fun getScriptAppend(): String {
    return fn.getStringAnnoValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::scriptAppend.name
    ) ?: ""
  }

  override fun getJoinAppend(): String {
    return fn.getStringAnnoValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::joinAppend.name
    ) ?: ""
  }

  override fun getWhereAppend(): String {
    return fn.getStringAnnoValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::whereAppend.name
    ) ?: ""
  }

  override fun getIncludesInner(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::includes.name
    )
  }

  override fun getExcludes(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::excludes.name
    )
  }

  override fun getLimit(): String? {
    return fn.getStringAnnoValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::limit.name
    )
  }

  override fun getUpdateSetList(): List<String> {
    return fn.getStringListAnnotationValue(
        KtMethodWrapper.METHOD_ANNO,
        XmllessMethod::updateSet.name
    )
  }

}
